#!/bin/bash

SOFT_PATH="/home/soft/"
NODE_PATH="/usr/local/node-v7.6.0-linux-x64/bin"
APP_HOME="/home/server/DXHQuestServer"
LOG_HOME="/home/logs/DXHQuestServer"
APP_LOG_PATH="$LOG_HOME/app"
PM2_GUI_LOG_PATH="$LOG_HOME/pm2-gui"
APP_START_SHELL="$APP_HOME/shell/linux/dxh-test-start.sh"


if [ ! -d "$SOFT_PATH" ]
   then
　　mkdir -p "$SOFT_PATH"
fi

isFind () {
    while read LINE 
       do 
       if [[ $LINE == *$2* ]]
          then
          return true
       fi
       done < $1
     return false
} 
isFind /etc/profile $NODE_PATH

cd $SOFT_PATH  &&
wget http://nodejs.org/dist/v7.6.0/node-v7.6.0-linux-x64.tar.xz &&
tar -xvJf node-v7.6.0-linux-x64.tar.xz &&
cp -r node-v7.6.0-linux-x64 /usr/local &&

if [ $? == false ]
   then 
   echo "export PATH=$PATH:$NODE_PATH" >> /etc/profile 
fi

&&

source /etc/profile &&
npm install -g cnpm --registry=https://registry.npm.taobao.org &&
cnpm i pm2 -g &&
pm2 install pm2-logrotate &&
cnpm install pm2-gui -g &&
mkdir -p $APP_HOME &&
mkdir -p $APP_LOG_PATH &&
mkdir -p $PM2_GUI_LOG_PATH &&

isFind /etc/rc.local $APP_HOME

if [ $? == false ]
   then 
   echo "sh $APP_START_SHELL" >> /etc/rc.local && chmod +x /etc/rc.local
fi

